//
//  ControlTemplateViewController.m
//  Uhomebk
//
//  Created by XXJ on 15/12/11.
//  Copyright © 2015年 com.segi. All rights reserved.
//




#define IOS8 [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0

#import "ControlTemplateViewController.h"
//#import "ControlTemplateViewHomeservicesController.h"
#import "ControlTemplateViewLabelController.h"
#import "ControlTemplateViewRecordController.h"
#import "ControlTemplateViewProtocolController.h"
#import "ControlTemplateViewCheckController.h"
#import "ControlTemplateViewEnumController.h"
#import "ControlTemplateViewDateTimeController.h"
#import "ControlTemplateViewDateController.h"
#import "ControlTemplateViewPhotoController.h"
#import "ControlTemplateViewRadioController.h"
#import "ControlTemplateViewTextController.h"
#import "ControlTemplateViewTextViewController.h"
#import "SEGControlTemplateViewCommunityAreaViewController.h"
#import "ControlTemplateRadioHouseViewController.h"
#import "ControlTemplateViewEvaluationController.h"
#import "ControlTemplateViewEvaluateReplyController.h"
#import "ControlTemplateViewCommunityLocationController.h"
#import "ControlTemplateViewProblemClassController.h"
#import "ControlTemplateViewBadReplyController.h"

#import<objc/runtime.h>
#import "SEGTemplateManagerModel.h"
#import "SEGInputTypeView.h"
#import "SEGTemplateModel.h"

static CGFloat const kTagForBaseLine = 999439429874;

NSString * const widgetType = @"widgetType";

@interface ControlTemplateViewController () < ControlTemplateViewTextViewControllerDelegate, ControlTemplateViewPhotoControllerDelegate,ControlTemplateViewCheckControllerDelegate,ControlTemplateRadioHouseViewControllerDelegate,ControlTemplateViewEvaluateReplyControllerDelegate,ControlTemplateViewBadReplyControllerDelegate,ControlTemplateViewLabelControllerDelegate> {
    
#pragma mark ************************** 【所有控件类型定义】 ***************************
    /**【多行文本框控件  - textarea】 */
    ControlTemplateViewTextViewController* ttextViewVC;
    
    /**【单行文本框控件 - text】*/
    ControlTemplateViewTextController* ttextVC;

    /**【单选框控件 - radio】*/
    ControlTemplateViewRadioController* radioVC;
    
    /**【图片框 -photo】*/
    ControlTemplateViewPhotoController* photoVC;
    
    /**【日期控件 -date】*/
    ControlTemplateViewDateController* dateVC;
    
    /**【日期时间控件 -datetime】*/
    ControlTemplateViewDateTimeController* datetimeVC;
    
    /**【日期时间(没有秒)控件 -datetime 】*/
    ControlTemplateViewDateTimeController* dateTimeNoSecVC;
    
    /**【多选控件 -checkbox】*/
    ControlTemplateViewCheckController* checkboxVC;
    
    /**【二选一控件 -enum】*/
    ControlTemplateViewEnumController* enumVC;
    
    /**【录音控件 - record】*/
    ControlTemplateViewRecordController* recordVC;
    
    /**【协议控件 - equipment】*/
    ControlTemplateViewProtocolController* protocolVC;
    
    /**【显示控件 - label】*/
    ControlTemplateViewLabelController* labelVC;
    
//    /**【到家控件 - homeservices】*/
//    ControlTemplateViewHomeservicesController *homeservicesVC;
    
    /**【区域选择控件 - community_area】*/
    SEGControlTemplateViewCommunityAreaViewController *CommunityAreaVC;
   
    /**【房屋选择控件 - radioHouse】*/
    ControlTemplateRadioHouseViewController *radioHouseVC;
    
    /**【评价控件 -Evaluation】*/
    ControlTemplateViewEvaluationController* evaluationVC;
    
    /**评价及原因控件*/
    ControlTemplateViewEvaluateReplyController *evaluateReplyVC;
    
    /**【位置选择控件 -community_location】*/
    ControlTemplateViewCommunityLocationController *CommunitylocationVC;
    
    /**【问题归类控件 -problem_class】*/
    ControlTemplateViewProblemClassController *ProblemClassVC;
    
    /**差评原因控件*/
    ControlTemplateViewBadReplyController *badReplyVC;
    
    NSString *_planDetialID;
    NSInteger deleteNums;
    NSDictionary *checkDic;//巡更异常终止判断返回数据
    BOOL isAbort;
    BOOL isPatrolAbnormal;//标记巡更异常控件
    NSString *abnormalIds;//巡更异常类型IDs
    
    CGFloat heightScrollView;
}
@property (nonatomic, assign) __block NSInteger uploadCount; //记录上传文件的控件总数
@end

@implementation ControlTemplateViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:1];
    
    
}

#pragma mark  ****************************【创建所有控件】******************************
-(void)onCreateTemplateWithHandleType:(NSString *)type dataArr:(NSArray *)array view:(UIView *)view
{
    _handletype=type;
    [self onCreateTemplateView:array view:view];
}

- (ControlTemplateViewRadioController *)extracted {
    return [ControlTemplateViewRadioController sharedManagerRadio];
}

- (void)onCreateTemplateView:(NSArray*)array view:(UIView*)view;
{
    SEGTemplateManagerModelArray * ModelArray = [SEGTemplateManagerModelArray initWithDictionary:@{@"data":array}];
    NSMutableArray * templateManagerModelArray = [[NSMutableArray alloc] initWithArray:ModelArray.templateManagerModelArray];
    
    UIScrollView* aScrollView = (UIScrollView*)view;
    
    for (UIView* aview in aScrollView.subviews) {
        //兼容工单录入详情界面的预约时间控件
        if (aview.tag != 11111 && aview.tag != 22222 && aview.tag != 33333) {
            [aview removeFromSuperview];
        }
    }
    
    _shareaScrollView = aScrollView;
    _vcArray = [[NSMutableArray alloc] init];
    NSInteger disGroupId = 0;
    
    //控件分组和排序
    templateManagerModelArray = [self seqTemplateDisgroupWith:templateManagerModelArray];
    
    //保存设备计划设计ID
    NSString* deviceId = 0;
    for (int i = 0; i < [templateManagerModelArray count]; i++) {
        SEGTemplateManagerModel * model = [templateManagerModelArray objectAtIndex:i];
        if (i == 0) {
            if (model.disGroupid) {
                disGroupId = [model.disGroupid integerValue];
            }
        }
        
        if ([model.widgetType isEqualToString:@"device_control"]) {
            deviceId = model.attrValue;
        }
    }
    
    //用一个BOOL值记录 是否已换组  将前面一组的最后一个控件的分割线去掉
    BOOL isChangeGroup = NO;
    
    heightScrollView = 0 + _defaultHeight;
    
    //过滤掉，不可写，并且attrValue没有值的控件
    [templateManagerModelArray enumerateObjectsUsingBlock:^(SEGTemplateManagerModel *templateModel, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *templateDict = [templateModel transform_InTo_Dictionary];
        NSString *attrValue = templateDict[@"attrValue"]?templateDict[@"attrValue"]:@"";
        NSString *writeable = templateDict[@"writable"]?templateDict[@"writable"]:@"";
        BOOL isReadOnly = writeable.length == 0 || [writeable isEqualToString:@"0"];
        if (attrValue.length == 0 && isReadOnly ) {
            [templateManagerModelArray removeObjectAtIndex:idx];
        }
    }];
    
    //创建所有控件
    for (int i = 0; i < [templateManagerModelArray count]; i++) {
        SEGTemplateManagerModel * model = [templateManagerModelArray objectAtIndex:i];
        id arraydicformodel = [templateManagerModelArray objectAtIndex:i];
        NSMutableDictionary * modeldic = [[NSMutableDictionary alloc] init];
        if ([arraydicformodel isKindOfClass:[NSDictionary class]]) {
            modeldic = arraydicformodel;
        }else{
            modeldic = [[model transform_InTo_Dictionary] mutableCopy];
            [modeldic setObject:[modeldic valueForKey:@"Data_init"] forKey:@"initData"];
        }
        
        if(![[SEGUtils ValueToString:model.readable] isEqualToString:@"0"]){//不可读不显示，不创建控件
            
            //分组高度
            if (disGroupId != [SEGUtils ValueToInteger:model.disGroupid]) {
                
                isChangeGroup = YES;
                
                disGroupId = [SEGUtils ValueToInteger:model.disGroupid];
                
                if (![model.widgetType isEqualToString:@"handletype"]) {
                    
                    if ([model.widgetType isEqualToString:@"handler"] && [_handletype isEqualToString:@"patrol"]) {//这里handler为巡更对象详情异常类型，不渲染handler控件，否则会有分隔栏
                    }else{
                        UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, heightScrollView - 0.5, CGRectGetWidth(view.bounds), 10)];
                        aView.tag = kTagForBaseLine;
                        [aScrollView addSubview:aView];
                        UIView* lineViewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(aView.bounds), 0.5)];
                        lineViewTop.backgroundColor = kColor_L2;
                        
                        heightScrollView += CGRectGetHeight(aView.bounds);
                    }
                    
                }else{//handletype弃用
                    NSArray *keyValueArr = [model.attrValue componentsSeparatedByString:@","];
                    NSArray * keyArray = [keyValueArr[0] componentsSeparatedByString:@"="];
                    if ( [[keyArray objectAtIndex:0] isEqualToString:@"patrolStatusYC"] || [[keyArray objectAtIndex:0] isEqualToString:@"patrolStatusZC"]) {
                        
                        UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, heightScrollView - 0.5, CGRectGetWidth(view.bounds), 10)];
                        aView.tag = kTagForBaseLine;
                        [aScrollView addSubview:aView];
                        
                        UIView* lineViewDown = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(aView.bounds) - 0.5, CGRectGetWidth(aView.bounds), 0.5)];
                        lineViewDown.backgroundColor = kColor_L2;
                        [aView addSubview:lineViewDown];
                        
                        UIView* lineViewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(aView.bounds), 0.5)];
                        lineViewTop.backgroundColor = kColor_L2;
                        [aView addSubview:lineViewTop];
                        
                        heightScrollView += CGRectGetHeight(aView.bounds);
                    }
                }
            }
            
            UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, heightScrollView, CGRectGetWidth(aScrollView.bounds), 0)];
            aView.backgroundColor = [UIColor clearColor];
            [aView.layer setMasksToBounds:YES];
            
            if ([model.widgetType isEqualToString:@"text"]) {//单行文本
                ttextVC = [ControlTemplateViewTextController sharedManagerText];
                ttextVC.params = modeldic;
                ttextVC.masterVC = self.mastVC;
                [aView addSubview:ttextVC.view];
                
                if (ttextVC) {
                    [_vcArray addObject:ttextVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(ttextVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"textarea"]) {
                ttextViewVC = [ControlTemplateViewTextViewController sharedManagerTextView];
                ttextViewVC.automaticallyAdjustsScrollViewInsets = NO;
                ttextViewVC.fundelegate = self;
                ttextViewVC.params = modeldic;
                ttextViewVC.masterVC = self.mastVC;
                [aView addSubview:ttextViewVC.view];
                
                if (ttextViewVC) {
                    [_vcArray addObject:ttextViewVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(ttextViewVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"radio"] || [model.widgetType isEqualToString:@"option"] || [model.widgetType isEqualToString:@"enum"]) //单选控件
            {
                radioVC = [self extracted];
                radioVC.params = modeldic;
                radioVC.masterVC = self.mastVC;
                [aView addSubview:radioVC.view];
                if (radioVC) {
                    [_vcArray addObject:radioVC];
                }
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(radioVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"photo"] || [model.widgetType isEqualToString:@"camera"] || [model.widgetType isEqualToString:@"album"]) {
                photoVC = [ControlTemplateViewPhotoController sharedManagerPhoto];
                photoVC.fundelegate = self;
                photoVC.params = modeldic;
                photoVC.masterVC = self.mastVC;
                [aView addSubview:photoVC.view];
                if (photoVC) {
                    [_vcArray addObject:photoVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(photoVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"date"]) {
                dateVC = [ControlTemplateViewDateController sharedManagerDate];
                dateVC.params = modeldic;
                dateVC.masterVC = self.mastVC;
                [aView addSubview:dateVC.view];
                if (dateVC) {
                    [_vcArray addObject:dateVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(dateVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"datetime"]) //datetime
            {
                datetimeVC = [ControlTemplateViewDateTimeController sharedManagerDateTime];
                datetimeVC.params = modeldic;
                datetimeVC.masterVC = self.mastVC;
                [aView addSubview:datetimeVC.view];
                if (datetimeVC) {
                    [_vcArray addObject:datetimeVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(datetimeVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"datetime_nosecond"]) //datetime没有秒
            {
                dateTimeNoSecVC = [ControlTemplateViewDateTimeController sharedManagerDateTime];
                dateTimeNoSecVC.params = modeldic;
                dateTimeNoSecVC.masterVC = self.mastVC;
                [aView addSubview:dateTimeNoSecVC.view];
                if (dateTimeNoSecVC) {
                    [_vcArray addObject:dateTimeNoSecVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(dateTimeNoSecVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"checkbox"]) {
                checkboxVC = [ControlTemplateViewCheckController sharedManagerCheck];
                checkboxVC.params = modeldic;
                checkboxVC.fundelegate = self;
                checkboxVC.masterVC = self.mastVC;
                [aView addSubview:checkboxVC.view];
                if (checkboxVC) {
                    [_vcArray addObject:checkboxVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(checkboxVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"evaluate"]) {
                evaluationVC = [ControlTemplateViewEvaluationController sharedManagerEvaluation];
                evaluationVC.params = modeldic;
                evaluationVC.masterVC = self.mastVC;
                [aView addSubview:evaluationVC.view];
                if (evaluationVC) {
                    [_vcArray addObject:evaluationVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(evaluationVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"record"]) {
                recordVC = [ControlTemplateViewRecordController sharedManagerRecord];
                recordVC.params = modeldic;
                recordVC.masterVC = self.mastVC;
                [aView addSubview:recordVC.view];
                if (recordVC) {
                    [_vcArray addObject:recordVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(recordVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"protocol"]) {
                protocolVC = [ControlTemplateViewProtocolController sharedManagerProtocol];
                protocolVC.params = modeldic;
                protocolVC.masterVC = self.mastVC;
                [aView addSubview:protocolVC.view];
                if (protocolVC) {
                    [_vcArray addObject:protocolVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(protocolVC.bgMainView.bounds));
            }

            else if ([model.widgetType isEqualToString:@"label"] || [model.widgetType isEqualToString:@"statustext"]) {
                labelVC = [[ControlTemplateViewLabelController alloc] init];
                labelVC.params = modeldic;
                labelVC.fundelegate = self;
                labelVC.masterVC = self.mastVC;
                [aView addSubview:labelVC.view];
                if (labelVC) {
                    [_vcArray addObject:labelVC];
                }
                
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(labelVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"community_area"])//区域选择控件
            {
                
                CommunityAreaVC = [SEGControlTemplateViewCommunityAreaViewController sharedManagerCommunityArea];
                CommunityAreaVC.params = modeldic;
                CommunityAreaVC.masterVC = self.mastVC;
                [aView addSubview:CommunityAreaVC.view];
                if (CommunityAreaVC) {
                    [_vcArray addObject:CommunityAreaVC];
                }
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(CommunityAreaVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"radiohouse"])//房屋选择控件
            {
                radioHouseVC = [ControlTemplateRadioHouseViewController sharedManagerRadioHouse];
                radioHouseVC.params = modeldic;
                radioHouseVC.fundelegate = self;
                radioHouseVC.masterVC = self.mastVC;
                [aView addSubview:radioHouseVC.view];
                if (radioHouseVC) {
                    [_vcArray addObject:radioHouseVC];
                }
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(radioHouseVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"evaluate_reply"])//评价及原因控件
            {
                evaluateReplyVC = [ControlTemplateViewEvaluateReplyController sharedManagerEvaluateReply];
                evaluateReplyVC.params = modeldic;
                evaluateReplyVC.masterVC = self.mastVC;
                evaluateReplyVC.evaluateReplyDelegate = self;
                [aView addSubview:evaluateReplyVC.view];
                if (evaluateReplyVC) {
                    [_vcArray addObject:evaluateReplyVC];
                }
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(evaluateReplyVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"community_location"])//位置选择控件
            {
                CommunitylocationVC = [ControlTemplateViewCommunityLocationController sharedManagerCommunityLocation];
                CommunitylocationVC.params = modeldic;
                CommunitylocationVC.masterVC = self.mastVC;
                [aView addSubview:CommunitylocationVC.view];
                [_vcArray addObject:CommunitylocationVC];
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(CommunitylocationVC.bgMainView.bounds));
            }
            else if ([model.widgetType isEqualToString:@"problem_class"])//问题归类控件
            {
                ProblemClassVC = [ControlTemplateViewProblemClassController sharedManagerProblemClass];
                ProblemClassVC.params = modeldic;
                ProblemClassVC.masterVC = self.mastVC;
                [aView addSubview:ProblemClassVC.view];
                [_vcArray addObject:ProblemClassVC];
                aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(ProblemClassVC.bgMainView.bounds));
                
            }
            else if ([model.widgetType isEqualToString:@"bad_reply"])//差评控件
            {
                //只读的话直接展示label控件
                if ([model.writable isEqualToString:@"0"]) {
                    labelVC = [[ControlTemplateViewLabelController alloc] init];
                    labelVC.params = modeldic;
                    labelVC.fundelegate = self;
                    labelVC.masterVC = self.mastVC;
                    [aView addSubview:labelVC.view];
                    
                    [_vcArray addObject:labelVC];
                    
                    aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(labelVC.bgMainView.bounds));
                }
                else{
                    badReplyVC = [ControlTemplateViewBadReplyController sharedManagerBadReply];
                    badReplyVC.params = modeldic;
                    badReplyVC.masterVC = self.mastVC;
                    badReplyVC.badReplyDelegate = self;
                    [aView addSubview:badReplyVC.view];
                    [_vcArray addObject:badReplyVC];
                    aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(badReplyVC.bgMainView.bounds));
                }
                
            }
            else{//不识别的控件全部用text
                if([model.writable isEqualToString:@"1"]) {
                    ttextVC = [ControlTemplateViewTextController sharedManagerText];
                    ttextVC.params = modeldic;
                    ttextVC.masterVC = self.mastVC;
                    [aView addSubview:ttextVC.view];
                    
                    [_vcArray addObject:ttextVC];
                    
                    aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(ttextVC.bgMainView.bounds));
                } else {
                    labelVC = [[ControlTemplateViewLabelController alloc] init];
                    labelVC.params = modeldic;
                    labelVC.fundelegate = self;
                    labelVC.masterVC = self.mastVC;
                    [aView addSubview:labelVC.view];
                    
                    [_vcArray addObject:labelVC];
                    
                    aView.frame = CGRectMake(0, aView.frame.origin.y, CGRectGetWidth(aView.bounds), CGRectGetHeight(labelVC.bgMainView.bounds));
                }
            }
            
            heightScrollView += CGRectGetHeight(aView.bounds);
            [aScrollView addSubview:aView];
            
            if (isChangeGroup)//如果已经换组 找出上一组的最后一个控件 去除下划线
            {
                [self hideLastControlViewLineView:_vcArray isChangeGroup:YES];
                isChangeGroup = NO;
            }
        }
        
    }
    //所有控件创建完成之后找到最后一个控件  去除下划线
    [self hideLastControlViewLineView:_vcArray isChangeGroup:NO];
    
    self.tempViewHeight = heightScrollView;
    
    aScrollView.contentSize = CGSizeMake(0, heightScrollView + 44);
    
    
}

/**
 对控件进行分组和排序

 @param templateArray 原始数据
 @return 排序之后的数据
 */
-(NSMutableArray *)seqTemplateDisgroupWith:(NSMutableArray *)templateArray{
    NSMutableArray *seqData = [[NSMutableArray alloc] initWithArray:templateArray];
    if (templateArray.count == 0) {
        return seqData;
    }
    //按照组冒泡排序
    for (int  i =0; i<[seqData count]-1; i++) {
        
        for (int j = i+1; j<[seqData count]; j++) {
            SEGTemplateModel *model_I = seqData[i];
            SEGTemplateModel *model_J = seqData[j];
            if ([model_I.disGroupid intValue]>[model_J.disGroupid intValue]) {
                //交换
                [seqData exchangeObjectAtIndex:j withObjectAtIndex:i];
            }
        }
    }
    
    //按照seq冒泡排序
    for (int  i =0; i<[seqData count]-1; i++) {
        
        for (int j = i+1; j<[seqData count]; j++) {
            SEGTemplateModel *model_I = seqData[i];
            SEGTemplateModel *model_J = seqData[j];
            if ([model_I.seq intValue]>[model_J.seq intValue] && [model_I.disGroupid intValue] == [model_J.disGroupid intValue]) {
                //交换
                [seqData exchangeObjectAtIndex:j withObjectAtIndex:i];
            }
        }
    }
    return seqData;
}

//隐藏每组最后一个(和整个页面最后一个)控件的下划线
-(void)hideLastControlViewLineView:(NSArray *)vcArray isChangeGroup:(BOOL)isChangeGroup
{
    if (vcArray.count == 0)
    {
        return;
    }
    
    //每组最后一个下标-2 页面最后一个下标-1
    NSInteger index = isChangeGroup?vcArray.count-2:vcArray.count-1;
    
    //保证index不小于0
    index = index>0?index:0;
    
    //找出lineView
    UIViewController *vc = vcArray[index];
    
    //记录VC的值
    if (vc.tempBgView)
    {
        UIView *lineView = [vc.tempBgView viewWithTag:8888];
        if (lineView)
        {
            lineView.hidden = YES;
        }
    }
    
    
}


#pragma mark -可写必填项校验

/**
 检验控件必填项
 @param optional  1-必填 0-非必填
 */
- (BOOL)onErrorMessage:(NSDictionary*)dict optional:(NSString*)optional checkRequired:(BOOL)checkRequired
{
    //不需要校验
    if (checkRequired == NO) {
        return YES;
    }
    
    //不可写不做必填项校验
    if (![[SEGUtils ValueToString:[dict valueForKey:@"writable"]] isEqualToString:@"1"]) {
        return YES;
    }
    //非必填项校验 - 不需要检验必填项
    if ([[SEGUtils ValueToString:[dict valueForKey:@"required"]] isEqualToString:@"0"]) {
        return YES;
    }
    
    //需要做必填项校验 - 已填写
    if ([optional isEqualToString:@"0"]) {
        return YES;
    }
    
    //需要做必填项校验 - 未填写
    [SEGAlertView showTmepMessageWithMessage:[dict valueForKey:@"markedWords"]];
    
    if ([[dict valueForKey:@"markedWords"] isEqualToString:@""]) {
        [SEGAlertView showTmepMessageWithMessage:@"信息填写不完整"];
    }
    isSuccess = NO;
    return NO;
    
}

#pragma mark  ********【获取所有控件的值(控件VlaueStr属性) - 并且做是否必填校验】**********
/**
 *  返回所有控件值
 *  @param checkRequired - 是否检测控件必填  YES - 检测  NO-不检测
 *  @return 返回数据字典
 */
- (NSDictionary*)onReponseTemplateValueWithCheckRequired:(BOOL)checkRequired
{
//    "variables":[{ // 表单输入内容
//        "attrInstId":""，//属性实例ID
//        "attrValue":"" //属性值
//    }]
    NSMutableArray * variables = [[NSMutableArray alloc] init];
    
    isSuccess = YES; //用于校验必填项是否已填写
    NSMutableDictionary * params = [[NSMutableDictionary alloc] init];

    for (int i = 0; i < [_vcArray count]; i++) {
        NSDictionary * oneVariable = [[NSDictionary alloc] init];
        NSString * attrInstId = @"";
        NSString * attrValue = @"";

        if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewTextViewController class]]) //多行文本
        {
            ControlTemplateViewTextViewController* tempVC = [_vcArray objectAtIndex:i];
            
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) //修正isSuccess的值
            {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewTextController class]]) //单行文本
        {
            ControlTemplateViewTextController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                } 
            }
            else {
                break;
            }
        }
       
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRadioController class]]) //单选控件
        {
            ControlTemplateViewRadioController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewPhotoController class]]) //图相框控件
        {
            ControlTemplateViewPhotoController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewDateController class]]) //日期控件
        {
            ControlTemplateViewDateController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewDateController class]]) //日期控件
        {
            ControlTemplateViewDateController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewDateTimeController class]]) //日期时间控件
        {
            ControlTemplateViewDateTimeController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewCheckController class]]) //多选控件
        {
            ControlTemplateViewCheckController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewEnumController class]]) //二选一控件
        {
            ControlTemplateViewEnumController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
       else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRecordController class]]) //录音控件
        {
            ControlTemplateViewRecordController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewProtocolController class]]) //协议控件
        {
            ControlTemplateViewProtocolController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewLabelController class]]) //显示控件
        {
            ControlTemplateViewLabelController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[SEGControlTemplateViewCommunityAreaViewController class]]) //区域选择
        {
            SEGControlTemplateViewCommunityAreaViewController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateRadioHouseViewController class]]) //房屋选择
        {
            ControlTemplateRadioHouseViewController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                    _houseId = [SEGUtils dictionaryWithJsonString:attrValue][@"id"];
                    _houseInfo = [SEGUtils dictionaryWithJsonString:attrValue][@"value"];
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewEvaluationController class]]) //评价控件
        {
            ControlTemplateViewEvaluationController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                    _evaluateValue = attrValue;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewEvaluateReplyController class]])//评价及原因控件
        {
            ControlTemplateViewEvaluateReplyController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                    _evaluateValue = [SEGUtils dictionaryWithJsonString:attrValue][@"evaluate"];;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewCommunityLocationController class]]) //位置选择
        {
            ControlTemplateViewCommunityLocationController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewProblemClassController class]]) //问题归类
        {
            ControlTemplateViewProblemClassController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewBadReplyController class]])//差评控件
        {
            ControlTemplateViewBadReplyController* tempVC = [_vcArray objectAtIndex:i];
            if ([self onErrorMessage:tempVC.params optional:tempVC.optional checkRequired:checkRequired]) {
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
                    attrInstId = [SEGUtils ValueToString:tempVC.param_id];
                    attrValue = tempVC.valueStr;
                }
            }
            else {
                break;
            }
        }

        oneVariable  = [NSDictionary dictionaryWithObjects:@[attrInstId,attrValue] forKeys:@[@"attrInstId",@"attrValue"]];
        [variables addObject:oneVariable];
    }
    
    [params setValue:variables forKey:@"variables"];
    
    if (isSuccess) //是否所有数据校验成功
    {
        [params setValue:@"0" forKey:@"code"];
    }
    else {
        [params setValue:@"-1" forKey:@"code"];
    }
    NSLog(@"所有控件的值 = %@",params);
    return params;
}



#pragma mark  ********【进入详情向控件填充缓存数据】************************
/**
 *  向控件填充数据
 *
 *  @param data 接收数据json
 */
- (void)paddingOrderDetail:(id)data
{
    NSDictionary* dict = data;

    for (NSString* keyAll in [dict allKeys]) {
        for (int i = 0; i < [_vcArray count]; i++) {
            if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewTextViewController class]]) //多行文本
            {
                ControlTemplateViewTextViewController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingTextView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewTextController class]]) //单行文本
            {
                ControlTemplateViewTextController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingTextView:[dict valueForKey:keyAll]];
                }
            }

            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRadioController class]]) //单选控件
            {
                ControlTemplateViewRadioController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingRadio_OptionView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewPhotoController class]]) //图片框控件
            {
                ControlTemplateViewPhotoController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                  
                    [tempVC paddingPhotoView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewDateController class]]) //日期控件
            {
                ControlTemplateViewDateController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingDateView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewDateTimeController class]]) //日期时间控件
            {
                ControlTemplateViewDateTimeController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingDateTimeView:[dict valueForKey:keyAll]];
                }
            }
//            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRadioHouseController class]]) //特殊控件
//            {
//                ControlTemplateViewRadioHouseController* tempVC = [_vcArray objectAtIndex:i];
//                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"writable"]] isEqualToString:@"1"]) {
//                    [tempVC paddingRadioHouseView:[dict valueForKey:keyAll]];
//                }
//            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewCheckController class]]) //多选控件
            {
                ControlTemplateViewCheckController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingCheckBoxView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewEnumController class]]) //二选一控件
            {
                ControlTemplateViewEnumController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingEnumView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRecordController class]]) //录音控件
            {
                ControlTemplateViewRecordController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingRecordView:[dict valueForKey:keyAll]];
                }
            }
            else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewProtocolController class]]) //协议控件
            {
                ControlTemplateViewProtocolController* tempVC = [_vcArray objectAtIndex:i];
                if ([[SEGUtils ValueToString:[tempVC.params valueForKey:@"attrInstId"]] isEqualToString:keyAll]) {
                    [tempVC paddingProtocolView:[dict valueForKey:keyAll]];
                }
            }
        }
    }
    
}


- (void)checkDevice
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_CHECK_DEVICE" object:nil userInfo:nil];
}

#pragma mark - ================<定制化APP版本控件文件上传(文件、录音、签名、评论)> start===============

/**
 定制化版本上传控件上的图片，录音，文件等资源

 @param params 所有控件保存的值 paramId - value
 @param uploadResult 上传结果回调 YES-上传成功，或者没有需要上传的  NO-上传失败
 */
-(void)SEGUploadFileControlTemplate:(NSDictionary*)params uploadResult:(uploadFilesResultCallBack)uploadResult
{
    self.uploadResult = uploadResult;
    self.uploadCount = [self checkUploadTemplateNum];//初始值为0
    if (self.uploadCount == 0) {//没有需要上传的
        self.uploadResult(YES, @"没有需要上传的文件");
        return;
    }
    
    for (int i = 0; i < [_vcArray count]; i++) {
        //1.找到对应的控件
        if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewPhotoController class]]) //相册&相机
        {
            ControlTemplateViewPhotoController* tempVC = [_vcArray objectAtIndex:i];
            
            //2.以现有的图片为准，之前的图片路径全部清除
            if ([tempVC.picDataArray count] > 0) {
                tempVC.valueStr = @""; //清除图片值
                //3.上传图片
                [self uploadImageWithImageArray:tempVC.picDataArray params:params tempVC:tempVC];
            }
           
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRecordController class]]) //录音文件
        {
            ControlTemplateViewRecordController* tempVC = [_vcArray objectAtIndex:i];
            if ([tempVC.soundDataArray count] > 0) {
                tempVC.valueStr = @""; //清除录音值
                //3.上传录音
                [self uploadRecordWithRecordArray:tempVC.soundDataArray params:params tempVC:tempVC];
            }
        }
    }

}


/**
 获取需要上传文件的控件总数
 */
-(NSInteger)checkUploadTemplateNum
{
    self.uploadCount = 0;
    
    for (int i = 0; i < [_vcArray count]; i++) {
        if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewPhotoController class]]) //相册&相机
        {
            ControlTemplateViewPhotoController* tempVC = [_vcArray objectAtIndex:i];
            if ([tempVC.picDataArray count] > 0) {
                self.uploadCount ++;
            }
        }
        else if ([[[_vcArray objectAtIndex:i] class] isEqual:[ControlTemplateViewRecordController class]]) //录音文件
        {
            ControlTemplateViewRecordController* tempVC = [_vcArray objectAtIndex:i];
            if ([tempVC.soundDataArray count] > 0) {
                self.uploadCount ++;
            }
        }
    }
    
    return self.uploadCount;
}

/**
 上传图片
 */
-(void)uploadImageWithImageArray:(NSMutableArray *)imageArray params:(NSDictionary *)params tempVC:(UIViewController *)tempVC
{
    // 注:这里传过来的是图片的路径数组，不是UIImage数组
    WeakSelf;
    [[SEGMultipartfileuploadManager sharedManager] requestMultipartfileuploadWithImagePaths:imageArray progress:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            NSString *messageStr = result[@"message"] ? result[@"message"]:kNetworkErrorMessage;
            self.uploadResult(NO,messageStr);
            return;
        }
        
        //成功处理
        weakSelf.uploadCount -- ;  //完成一个
        //控件相关逻辑处理，本地缓存在方法内部
        NSDictionary * dic = (NSDictionary *)result;
        NSArray * picPathsArr = dic[@"data"];
        [weakSelf newUploadQueueFinished:picPathsArr params:params viewVC:tempVC];
        NSLog(@"AF批量上传图片成功");
    }];
    
    
    
    /* 拆包之前的逻辑
    //上传图片
    [[[SEGUpLoadDataManager alloc] init] SEGUpLoadImageWithImagesData:imageArray andDatasPath:params nextFinished:^(SEGHTTPSessionManagerResponseType responseType, double progressValue, BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (responseType == PROGRESS) {
            
        }else if (responseType == RESULT)
        {
            //上传失败
            if (![[SEGUtils ValueToString:result[@"code"]] isEqualToString:@"0"]) {
                NSString *messageStr = result[@"message"]?result[@"message"]:kNetworkErrorMessage;
                self.uploadResult(NO,messageStr);
                return;
            }
            //成功处理
            uploadCount -- ;  //完成一个
            //控件相关逻辑处理，本地缓存在方法内部
            NSDictionary * dic = (NSDictionary *)result;
            NSArray * picPathsArr = dic[@"data"];
            [self newUploadQueueFinished:picPathsArr params:params viewVC:tempVC];
            NSLog(@"AF批量上传图片成功");
        }
    }];
    */
}

-(void)uploadRecordWithRecordArray:(NSMutableArray *)RecordArray params:(NSDictionary *)params tempVC:(UIViewController *)tempVC
{
    WeakSelf;
    NSData *recordData = [NSData dataWithContentsOfFile:[RecordArray objectAtIndex:0]];
    [[SEGUploadmultipartfileManager sharedManager] requestVoiceWithData:recordData progress:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            NSDictionary * dic = (NSDictionary *)result;
            NSArray * recordPathsArr = dic[@"data"];
            weakSelf.uploadCount -- ;  //完成一个
            [weakSelf newUploadQueueFinished:recordPathsArr params:params viewVC:tempVC];
            NSLog(@"AF上传录音成功");
        } else {
            weakSelf.uploadResult(NO,result[@"message"]);
            NSLog(@"%@",errorMessage);
        }
    }];
    
    /*拆包之前的逻辑
    //录音文件只有一个
    [[[SEGUpLoadDataManager alloc] init] SEGUpLoadRecordWithRecordData:[RecordArray objectAtIndex:0] andDatasPath:params nextFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            if ([codeType isEqualToString:@"0"]) {
                NSDictionary * dic = (NSDictionary *)result;
                NSArray * recordPathsArr = dic[@"data"];
                 uploadCount -- ;  //完成一个
                [self newUploadQueueFinished:recordPathsArr params:params viewVC:tempVC];
                NSLog(@"AF上传录音成功");
               

            }else{
                self.uploadResult(NO,result[@"message"]);
                NSLog(@"%@",errorMessage);
            }
        }else{
            self.uploadResult(NO,result[@"message"]);
            NSLog(@"%@",errorMessage);
        }
    }];
     */

}
/**
 *  使用AFNetworking封装的方法上传图片，上传所有图片完成后，调用此方法，进行控件值填充
 *
 *  @param picPaths 服务器返回的路径
 *  @param params  控件信息的数据字典
 *  @param viewVC  控件视图片
 */
-(void)newUploadQueueFinished:(NSArray*)picPaths params:(NSDictionary*)params viewVC:(UIViewController*)viewVC
{
    //上传完成 拿到服务器的图片地址  在提交工单时将此地址上传给服务器
    NSString* picStr = @"";
    if (picPaths.count>0) {
        picStr = [picPaths yy_modelToJSONString];
    }
    
    if ([viewVC isKindOfClass:[ControlTemplateViewPhotoController class]]) {
        ControlTemplateViewPhotoController* tempVC = (ControlTemplateViewPhotoController*)viewVC;
        tempVC.valueStr = picStr;
    }
   
    else if ([viewVC isKindOfClass:[ControlTemplateViewRecordController class]]) {
        ControlTemplateViewRecordController* tempVC = (ControlTemplateViewRecordController*)viewVC;
        tempVC.valueStr = picStr;
    }
    //全部上传完成才回调
    if (self.uploadCount == 0) {
        self.uploadResult(YES, @"全部文件上传成功");
    }
    
}

#pragma mark - ================<定制化APP版本控件文件上传(文件、录音、签名、评论)> end===============

/**
 *  当前端的表音提交时，上传所有图片完成回设此方法，进行控件值填充
 *
 *  @param urlDict 服务器返回的路径
 *  @param params  控件信息的数据字典
 *  @param viewVC  控件视图片
 */
- (void)uploadQueueFinished:(NSDictionary*)urlDict params:(NSDictionary*)params viewVC:(UIViewController*)viewVC
{
    //上传完成 拿到服务器的图片地址  在提交工单时将此地址上传给服务器
    NSArray *picarr=[urlDict valueForKey:@"data"];
    NSString* picStr;
    if (picarr.count>0) {
        picStr=picarr[0];
    }

    if ([viewVC isKindOfClass:[ControlTemplateViewPhotoController class]]) {
        ControlTemplateViewPhotoController* tempVC = (ControlTemplateViewPhotoController*)viewVC;
        if ([tempVC.valueStr length] > 0) {
            tempVC.valueStr = [NSString stringWithFormat:@"%@,%@", tempVC.valueStr, picStr];
        }
        else {
            tempVC.valueStr = picStr;
        }
    }
}


/**
 *  当前端的表单提交时，上传图片完成回调此方法
 *
 *  @param urlDict 服务器返回的路径
 *  @param params  控件信息的数据字典
 *  @param viewVC  控件视图片
 */
- (void)uploadQueueFail:(NSDictionary*)urlDict params:(NSDictionary*)params viewVC:(UIViewController*)viewVC
{
    
}

/**
 *  当前端的表单提交时，上传录音完成回调此方法，进行控件值填充
 *
 *  @param fileStr 服务器返回的录音文件
 *  @param viewVC  录音控件视图
 */
- (void)onResponseValue:(NSString*)fileStr params:(NSDictionary*)params viewVC:(UIViewController*)viewVC
{
    if ([viewVC isKindOfClass:[ControlTemplateViewRecordController class]]) {
        ControlTemplateViewRecordController* tempVC = (ControlTemplateViewRecordController*)viewVC;
        tempVC.valueStr = fileStr;
        [tempVC.soundDataArray removeAllObjects];
    }
}

/**
 *  上传音频失败
 */
- (void)onFailResponseValue
{
}


#pragma mark -是否上传成功
- (void)uploadQueueFail:(NSDictionary*)urlDict imageView:(UIImageView*)imageView
{
}

- (void)uploadQueueFinished:(NSDictionary*)urlDict imageView:(UIImageView*)imageView
{
    
}

#pragma mark ********************************【控件回调】*******************************
#pragma mark -费用控件回调改变控件大小位置

- (void)onFeeRefeshUI
{

    UIScrollView* aScrollView = (UIScrollView*)_shareaScrollView;

    CGFloat heightView = 0;
    for (UIView* aView in aScrollView.subviews) {
        if (aView.tag != kTagForBaseLine) {
            if ([aView.subviews count] > 0) {
                UIView* bView = [aView.subviews objectAtIndex:0];
                if ([bView.subviews count] > 0) {
                    UIView* cView = [bView.subviews objectAtIndex:0];
                    aView.frame = CGRectMake(0, heightView, CGRectGetWidth(aView.bounds), CGRectGetHeight(cView.bounds));
                }
                //兼容服务类型自定义的控件增加10组件距
                if (aView.tag == 22222) {
                    heightView += 10;
                }
                heightView += CGRectGetHeight(aView.bounds);
            }
        }
        else {
            aView.frame = CGRectMake(0, heightView - 0.5, CGRectGetWidth(aView.bounds), CGRectGetHeight(aView.bounds));
            heightView += CGRectGetHeight(aView.bounds);
        }
    }

    aScrollView.contentSize = CGSizeMake(0, heightView + 44);
    self.tempViewHeight=heightView-55;
}

#pragma mark - 到家服务费文字大小改变回调
-(void)refreshHomeserviceWithCompelete
{
    
}

#pragma mark - 到家服务费改变控件大小回调
-(void)resfreshHomeservices
{
//    NSLog(@"======");
    //    [self.fundelegate onFeeRefeshUI];
    
    UIScrollView* aScrollView = (UIScrollView*)_shareaScrollView;
    
    CGFloat heightView = 0;
    for (UIView* aView in aScrollView.subviews) {
        if (aView.tag != kTagForBaseLine) {
            if ([aView.subviews count] > 0) {
                UIView* bView = [aView.subviews objectAtIndex:0];
                if ([bView.subviews count] > 0) {
                    UIView* cView = [bView.subviews objectAtIndex:0];
                    aView.frame = CGRectMake(0, heightView, CGRectGetWidth(aView.bounds), CGRectGetHeight(cView.bounds));
                    NSLog(@"avFrame ==== %@",NSStringFromCGRect(aView.frame));
                }
                heightView += CGRectGetHeight(aView.bounds);
            }
        }
        else {
            aView.frame = CGRectMake(0, heightView - 0.5, CGRectGetWidth(aView.bounds), CGRectGetHeight(aView.bounds));
            heightView += CGRectGetHeight(aView.bounds);
        }
    }
    
    aScrollView.contentSize = CGSizeMake(0, heightView + 44);
    NSLog(@"contenSize = %.0f",heightView + 44);
}

#pragma mark -清除通右
- (void)removeNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
